#!/bin/bash

################参数配置区################
#传输方式 （"local","sftp","scp"） 暂只有sftp提供下载功能
transport="sftp"

#SFTP传输方式 ("0"-从远程服务器拉取下载    "1"-上传到远程服务器)
trans_type="1"

#企业微信机器人配置
webHookUrl="http://10.132.7.11:9998/bp/Robot/cgi-bin/webhook/send?key=47a3cfc9-a4e6-48cf-ac09-ced39eb67ea8"

#远程服务器IP地址
target_ip="10.110.68.4"

#远程服务器访问端口
target_port="22"

#远程服务器登录用户名
user="ShangYang"

#远程服务器登录密码
password="1qaz@WSX"

#本地文件所在目录
localpath="/cfs_sy_p2/TA/output/Confirm/007/"

#远程目标路径
targetpath="/c:/send/zhaoshangyinhang-DX"

#指定文件名（不指定填空）
file_name=""

#是否自动填充路径中YYYYMMDD文件夹
date_folder="yes"

#本地日期文件夹子文件夹
a_folder=""

#上传时是否检查源文件.ok文件
exit_ok="no"

#目标路径是否生成.ok文件
new_ok="no"

#是否需要企业微信通知
wx_notice="yes"

#企业微信通知内容
wx_msg=""

########################################
########################################

#引入核心文件
source /home/gmyl/batch/core.sh

#ok方案
if [ "$exit_ok" = "yes" ]; then
	if [ "$new_ok" = "yes" ]; then
		ok_type="11"
	else
		ok_type="10"
	fi
else	
	if [ "$new_ok" = "yes" ]; then
		ok_type="01"
	else
		ok_type="00"
	fi
fi		
	
#判断是否自动填充YYYYMMDD文件夹
if [ "$date_folder" = "yes" ]; then
	localpath=${localpath}"/"${today}
	#判断子文件夹
	if [ "$a_folder" != ""  ]; then
		localpath=${localpath}"/"${a_folder}
	fi
	#如果直接上传文件夹，远程路径不需要拼接YYYYMMDD
	#if [ "$file_name" != "" -o "$ok_type" != "00" ]; then
		targetpath=${targetpath}"/"${today}
	#fi
fi	



#单个文件传输
if [ "$file_name" != "" ]; then
	echo "--准备进行单个文件传输 方式${transport}  ：${target_ip}  ${target_port}   ${localpath}  ${targetpath} ${ok_type}  ${file_name}"
	case ${transport} in
		"local")
			local_cp  ${localpath}"/"${file_name}   ${targetpath}  ${ok_type}
			;;
		"sftp")			
			sftp_file ${target_ip}  ${target_port}   ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type}  ${trans_type}  ${file_name}
			;;
		"scp")
			scp_file ${target_ip}   ${target_port}  ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type}  ${file_name}
			;;
		*)
			echo "请选择文件传输方式"
			exit
			;;
	esac	
else
	#文件夹传输 
	if [ "$ok_type" = "00" ]; then
	#不需要调用ok文件可批量传输
		echo "--准备直接进行文件夹传输：${target_ip}  ${target_port}   ${localpath}  ${targetpath} ${ok_type}  "
			case ${transport} in
				"local")
					local_cp  ${localpath}  ${targetpath}  ${ok_type}
					;;
				"sftp")			
					sftp_file ${target_ip}  ${target_port}   ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type}  ${trans_type} 
					;;
				"scp")
					scp_file ${target_ip}  ${target_port}   ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type} 
					;;
				*)
					echo "请选择文件传输方式"
					exit
					;;
			esac	
	else
		#SFTP下载直接拉取文件夹
		if [ "${transport}" = "sftp" -a  "${trans_type}" = "0" ]; then
			sftp_file ${target_ip}  ${target_port}   ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type}  ${trans_type} 
			#企业微信通知
			if [ "$wx_notice" = "yes" ]; then
				echo "企业微信通知。。。"
				robot_msg "$webHookUrl" "文件搬运任务执行完毕\n  >任务详情： ${wx_msg}"	
			fi
			exit
		fi
		#上传的话遍历本地文件夹		
		for f in `ls $localpath`
		do
			echo "遍历项------$localpath/$f"
           if [ -f "$localpath/$f" ]                       #   $dir/$f    保障路径正确                               
           then                                                                                      
              echo "--遍历文件夹，文件$localpath/$f 准备上传 $targetpath"         
			  case ${transport} in
				"local")
					local_cp  ${localpath}"/"${f}  ${targetpath}  ${ok_type}
					;;
				"sftp")			
					sftp_file ${target_ip}  ${target_port}   ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type}   ${trans_type}  ${f} 
					;;
				"scp")
					scp_file ${target_ip}   ${target_port}  ${user}  ${password}   ${localpath}  ${targetpath} ${ok_type}   ${f}
					;;
				*)
					echo "请选择文件传输方式"
					exit
					;;
			esac	
          elif [ -d "$localpath/$f" ]                                                                     
          then                                                                                      
              echo "$localpath/$f是目录"                                                                                                                   
          else                                                                                      
              echo "$localpath/$f未知"                                                                    
          fi                                                                                                                                                                              
		done  
	fi
fi

#企业微信通知
if [ "$wx_notice" = "yes" ]; then
	echo "企业微信通知。。。"
	robot_msg "$webHookUrl" "招商银行代销确认文件发送至深证通\n  >任务详情： ${wx_msg}"	
fi

